package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddLog;
import com.baizhi.dao.CategoryMapper;
import com.baizhi.dto.CategoryPageDTO;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Category;
import com.baizhi.entity.CategoryExample;
import com.baizhi.service.CategoryService;
import com.baizhi.util.UUIDUtil;
import com.baizhi.vo.CommonQueryPageVO;
import com.baizhi.vo.CommonVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Override
    public CommonQueryPageVO queryOnePage(PageDTO pageDTO) {

        //创建条件对象
        CategoryExample example = new CategoryExample();
        //example.createCriteria().andCateNameIsNull();
        example.createCriteria().andLevelsEqualTo(1);

        //根据条件查询一级类别的数量   条件： levels = 1
        int total = categoryMapper.selectCountByExample(example);

        RowBounds rowBounds = new RowBounds((pageDTO.getPage() - 1) * pageDTO.getPageSize(), pageDTO.getPageSize());

        //分页查询数据
        List<Category> categories = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);
        CommonQueryPageVO commonQueryPageVO = new CommonQueryPageVO();
        commonQueryPageVO.setPage(pageDTO.getPage());
        commonQueryPageVO.setTotal(total);
        commonQueryPageVO.setRows(categories);

        return commonQueryPageVO;
    }

    @AddLog(value = "添加类别")
    @Override
    public CommonVO add(Category category) {

        try {
            //判断添加的是一级类别还是二级类别
            if(category.getParentId() == null){
                //一级类别
                category.setLevels(1);
            }else{
                //二级类别
                category.setLevels(2);
            }
            category.setId(UUIDUtil.getUUID());

            //添加数据
            categoryMapper.insertSelective(category);
            return CommonVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.faild();
        }

    }
    @AddLog(value = "删除类别")
    @Override
    public CommonVO delete(Category category) {

        //判断删除的是一级类别还是二级类别
        if(category.getParentId() == null){

            //一级类别 判断该一级类别下是否有二级类别
            //根据一级类别id查询一级类别下是否有二级类别

            CategoryExample example = new CategoryExample();

            example.createCriteria().andParentIdEqualTo(category.getId());

            //查询你一级类别下二级类别的数据
            int count = categoryMapper.selectCountByExample(example);
            if(count == 0){
                // 没有 直接删除
                categoryMapper.delete(category);
                return CommonVO.success("一级类别删除成功！");
            }else{
                //有  不能删除
                return CommonVO.faild("该一级类别下有二级类别 不能删除！！");
            }
        }else{
            //二级类别  判断该二级类别下是否有视频
            // 有 不能删除
            // 没有  直接删除
            categoryMapper.delete(category);
            return CommonVO.success("二级类别删除成功1");
        }

    }

    @Override
    public Category queryById(String id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @AddLog(value = "修改类别信息")
    @Override
    public CommonVO update(Category category) {

        try {
            categoryMapper.updateByPrimaryKeySelective(category);
            return CommonVO.success("修改成功");

        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.success("修改失败");
        }
    }

    @Override
    public CommonQueryPageVO queryTwoPage(CategoryPageDTO categoryPageDTO) {



        //创建条件对象
        CategoryExample example = new CategoryExample();
        example.createCriteria().andParentIdEqualTo(categoryPageDTO.getCategoryId());

        //根据条件查询一级类别的数量   条件： levels = 1
        int total = categoryMapper.selectCountByExample(example);

        RowBounds rowBounds = new RowBounds((categoryPageDTO.getPage() - 1) * categoryPageDTO.getPageSize(), categoryPageDTO.getPageSize());

        //分页查询数据
        List<Category> categories = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);

        return new CommonQueryPageVO(categoryPageDTO.getPage(),total,categories);


    }

    @Override
    public List<Category> queryByLevelsCategory(Integer levels) {

        CategoryExample example = new CategoryExample();
        example.createCriteria().andLevelsEqualTo(levels);

        List<Category> categories = categoryMapper.selectByExample(example);

        return categories;
    }
}
